package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"helloCms/models"
	"time"
)

type ArticleClassController struct {
	beego.Controller
}

func (c *ArticleClassController)Index()  {
	c.TplName="ArticleClass/Index.html"
}

func (c *ArticleClassController)ShowArticleClass()  {
	o:=orm.NewOrm()
	articleClasses:=make([]models.Articleclass,0)
	o.QueryTable("articleclass").Filter("parent_id",0).OrderBy("Id").All(&articleClasses)
	c.Data["json"]=map[string]interface{}{"rows":articleClasses}
	c.ServeJSON()
}

func (c *ArticleClassController)ShowChildClass()  {
	cId,_:=c.GetInt("id")
	o:=orm.NewOrm()
	articleClasses:=make([]models.Articleclass,0)
	o.QueryTable("articleclass").Filter("parent_id",cId).OrderBy("Id").All(&articleClasses)
	c.Data["json"]=map[string]interface{}{"rows":articleClasses}
	c.ServeJSON()

}

func (c *ArticleClassController)ShowAddParentClass()  {
	c.TplName="ArticleClass/ShowAddParentClass.html"
}

func (c *ArticleClassController)AddParentClass()  {
	userId:=c.GetSession("userId").(int)

	className:=c.GetString("className")
	remark:=c.GetString("Remark")

	articleParentClass:=models.Articleclass{
		ClassName:className,
		Remark:remark,
		CreateUserId:userId,
		CreateDate:time.Now(),
		ParentId:0,
		DelFlag:0,
	}
	o:=orm.NewOrm()
	_,err:=o.Insert(&articleParentClass)
	if err!=nil{
		c.Data["json"]=map[string]interface{}{"flag":"no"}
	}else {
		c.Data["json"]=map[string]interface{}{"flag":"ok"}
	}
	c.ServeJSON()
}

func (c *ArticleClassController)ShowAddChildClass()  {
	cId,_:=c.GetInt("cId")
	articleClass:=models.Articleclass{Id:cId}
	o:=orm.NewOrm()
	o.Read(&articleClass)
	c.Data["classInfo"]=articleClass
	c.TplName="ArticleClass/ShowAddChildClass.html"
}

func (c *ArticleClassController)AddChildClass()  {
	userId:=c.GetSession("userId").(int)
	cId,_:=c.GetInt("cId")
	className:=c.GetString("className")
	remark:=c.GetString("Remark")

	articleChildClass:=models.Articleclass{
		ClassName:className,
		Remark:remark,
		CreateUserId:userId,
		CreateDate:time.Now(),
		ParentId:cId,
		DelFlag:0,
	}
	o:=orm.NewOrm()
	_,err:=o.Insert(&articleChildClass)
	if err!=nil{
		c.Data["json"]=map[string]interface{}{"flag":"no"}
	}else {
		c.Data["json"]=map[string]interface{}{"flag":"ok"}
	}
	c.ServeJSON()
}